let app = getApp();
let starUrl = app.globalData.fileUrl + 'd974f42cf782aac0d88da7313fe3af04_26_26.jpeg';
let blackStarUrl = app.globalData.fileUrl + '4f1e2758b6896f0ebb23768b7be5875c_26_26.jpeg';
let innerAudioContext = ''
Page({
  data: {
    anchorsList: [],
    title: '',
    showAuthModal: false,
    audioIcon: 'http://image.tanwancat.com/aa6af4c843fc05169491106ca46c8053_20_20.jpeg',
    hasNext: false,
    hidden: false,
  },
  onLoad: function (options) {
    // let anchorInfo = vData.find(item => item.card_type ==='SECTION');
    // anchorInfo.items.forEach(item=>{
    //   item.audio.duration_res = item.audio.duration/1000;
    //   item.audio.isPlay=true;
    // })
    this.getData();
  },
  getData () {
    let ajax = getApp().globalData.ajax;
    ajax('homepage/feed').then(res => {
      if(res.code === -1){
        wx.showToast({
          title: '请求超时',
          icon: 'none',
          duration: 1000,
          mask: true
        })
        return
      }
      let anchorsList = [];
      res.data || [];
      res.data.forEach(item => {
        if (item.card_type === 'SECTION') {
          anchorsList.push(item);
        }
      })
      anchorsList.forEach(aItem => {
        aItem.items.forEach(item => {
          let heatNumber = Number(item.heat),
            heatList = [];
          for (let i = 1; i <= 5; i++) {
            if (i <= heatNumber) {
              heatList.push(starUrl);
            } else {
              heatList.push(blackStarUrl);
            }
          }
          item.heatList = heatList;
        })
      })
      this.setData({
        anchorsList,
        hidden: true,
      })
      wx.stopPullDownRefresh();
    })
  },
  toDetail (e) {
    let categoryInfo = e.target.dataset.categoryinfo;
    wx.navigateTo({
      url: `../anchors/anchors?categoryInfo=${categoryInfo}`,
      success: function () {
      },
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  audioPlay (e) {
    let src = e.currentTarget.dataset.src
    if (innerAudioContext.src === src) {
      if (innerAudioContext.paused) {
        innerAudioContext.play()
      } else {
        innerAudioContext.pause()
      }
    } else {
      innerAudioContext.src = src
    }
  },
  seeMoreClick (e) {
    let categoryInfo = JSON.stringify(e.target.dataset.categoryinfo)
    wx.navigateTo({
      url: `../anchors/anchors?categoryInfo=${categoryInfo}`,
    })
  },
  toSkillDetail (e) {
    wx.navigateTo({
      url: `../skillDetail/skillDetail?user_skill_id=${e.currentTarget.dataset.id}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onPause(() => {
      console.log('暂停播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.destroy()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.destroy()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})